package com.frank.leetcode.question_16_20;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/
 * 17. 电话号码的字母组合
 * 难度: 中等
 * <p>
 * <p>
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 * <p>
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 如图：https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Telephone-keypad2.svg/200px-Telephone-keypad2.svg.png
 * 示例:
 * <p>
 * 输入："23"
 * 输出：["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
 * 说明:
 * 尽管上面的答案是按字典序排列的，但是你可以任意选择答案输出的顺序。
 * <p>
 * <p>
 * Created by zhy on 2018/8/15.
 */
public class LetterCombinations {
    private static Logger LOG = LoggerFactory.getLogger(LetterCombinations.class);

    @Test
    public void run() {
        System.out.println(letterCombinations("9"));
    }


    private List<String> letterCombinations(String digits) {
        if (digits == null || digits.trim().equals("")){
            return Collections.emptyList();
        }
        char[][] c = new char[][]{{},{}, {'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};

        char[] array = digits.toCharArray();

        int len = 1;
        for (int i = 0; i < array.length; i++) {
            len *= c[array[i] - 48].length;
        }

        String[] sb = new String[len];

        int m = sb.length;
        for (int i = 0; i < array.length; i++) {
            char[] s = c[array[i] - 48];
            m = m / s.length;
            int index = 0;
            int n = 0;
            int count = 0;
            while (index < sb.length) {
                if (count == m) {
                    n++;
                    count = 0;
                    if (n == s.length) {
                        n = 0;
                    }
                }

                if (sb[index] == null) {
                    sb[index] = s[n] + "";
                } else {
                    sb[index] += s[n];
                }
                index++;
                count++;
            }
        }
        return Arrays.asList(sb);
    }


}